DEFINE STREAM bf .
DEFINE VARIABLE v_id LIKE code_cmmt .
DEFINE  VARIABLE v_num AS INTE . 
DEFINE VARIABLE fnameb AS CHAR .
DEFINE VARIABLE v_date AS CHAR .
DEFINE VARIABLE v_date1 AS CHAR .
DEFINE BUFFER cdmstr FOR code_mstr.
define variable voutput as character .
/* assign voutput = "d:\" .  */

 IF SEARCH(fnameb) <> ? THEN
    OS-DELETE VALUE(fnameb). 

assign voutput = "\\192.168.12.12\shared$\output\" .
fnameb = voutput + "fpxbak" + ".csv"  .
FIND FIRST cdmstr WHERE code_fldname = "fpxbak"
                   AND CODE_value = "fpx"  NO-LOCK NO-ERROR .
IF AVAIL cdmstr  THEN     DO:
   
    v_date = substr(trim(cdmstr.code_cmmt),1,2) + "/" + substr(trim(cdmstr.code_cmmt),4,2) +  "/" +  substr(trim(cdmstr.code_cmmt),7,2) .
END.

/* declare  v_tot cursor  FOR                           */
/* SELECT COUNT(*) FROM fpx_mstr  WHERE fpx_date> v_date . */
/* OPEN v_tot .                                         */
/* FETCH v_tot INTO v_num .                             */
/* CLOSE v_tot .                                        */

      output stream bf to value(fnameb) .
      PUT STREAM bf "fpx_part,fpx_site ,fpx_start,fpx_cust,fpx_date,fpx_days1,fpx_days2,fpx_days3,fpx_days4,fpx_days5,fpx_days6,fpx_days7,fpx_days8,fpx_days9,fpx_days10,fpx_days11,fpx_days12,fpx_days13,fpx_days14,fpx_days15,fpx_days16,fpx_days17,fpx_days18,fpx_days19,fpx_days20,fpx_days21,fpx_days22,fpx_days23,fpx_days24,fpx_days25,fpx_days26,fpx_days27,fpx_days28,fpx_days29,fpx_days30,fpx_days31,fpx_days32,fpx_days33,fpx_days34,fpx_days35,fpx_status,fpx_weeks1,fpx_weeks2,fpx_weeks3,fpx_weeks4,fpx_weeks5,fpx_weeks6,fpx_weeks7,fpx_weeks8,fpx_weeks9,fpx_weeks10,fpx_weeks11,fpx_weeks12,fpx_weeks13,fpx_weeks14,fpx_weeks15,fpx_weeks16,fpx_months1,fpx_months2,fpx_months3,fpx_months4,fpx_months5,fpx_months6,fpx_mon_tot1,fpx_mon_tot2,fpx_mon_tot3,fpx_mon_tot4,fpx_mon_tot5,fpx_mon_tot6,fpx_userid,fpx_type"  SKIP .
      FOR  EACH fpx_mstr NO-LOCK WHERE fpx_date > DATE(v_date)   BREAK BY fpx_date  :
           EXPORT STREAM bf DELIMITER "," fpx_part fpx_site  fpx_start  fpx_cust fpx_date  fpx_days fpx_status  fpx_weeks fpx_months fpx_mon_tot fpx_userid fpx_type .
           IF LAST(fpx_date) THEN  DO:
                v_date1 = SUBstr(STRING(fpx_date,"99999999"),3,2) + "/" + SUBstr(STRING(fpx_date,"99999999"),5,2)  + "/" + SUBstr(STRING(fpx_date,"99999999"),7,2).
                FIND first CODE_mstr WHERE CODE_fldname = "fpxbak"
                                      AND CODE_value = "fpx"
                                      EXCLUSIVE-LOCK NO-ERROR .
                    IF NOT AVAIL CODE_mstr  THEN  DO:
                       CREATE CODE_mstr.
                       ASSIGN  CODE_fldname = "fpxbak"
                               code_value   = "fpx"
                               code_cmmt    = v_date1 .
                    END.
                    ELSE DO:
                        CODE_cmmt = v_date1 .
                    END.
               END.
      END.
      OUTPUT STREAM bf CLOSE .
      QUIT . 
